﻿using System.Windows;
using System.Windows.Controls;

using GalaSoft.MvvmLight.Messaging;

using Rio.Framework.Pages;
using Rio.Manager.Application.Utils;
using Telerik.Windows.Controls.GridView;
using System.Windows.Data;
using Telerik.Windows.Controls;
using NetBay.Core.BusinessEntities.Workflow;
using System.Collections.Generic;
using System.Linq;
using Rio.Manager.Application.ViewModel.FolderTypeAdministration;
using System;
using Telerik.Windows.Controls.TreeListView;


namespace Rio.Manager.Application.Views
{
    public partial class ManageWorkflowTabView : UserControl
    {
        #region Constructors

        public ManageWorkflowTabView()
        {
            InitializeComponent();


            Dispatcher.BeginInvoke((Action)(() =>
            {
                WorkflowDefinitionsGridView.ExpandAllHierarchyItems();
            }));

            Unloaded += (s, e) =>
            {
                Messenger.Default.Unregister<RioDialogMessage>(this, ShowDialog);
            };

            Messenger.Default.Register<RioDialogMessage>(this, ShowDialog);
        }

        #endregion Constructors

        #region Methods

        /// <summary>
        /// Shows the dialog.
        /// </summary>
        /// <param name="msg">The MSG.</param>
        private void ShowDialog(RioDialogMessage msg)
        {
            MessageBoxChildWindow confirmWindow = new MessageBoxChildWindow(msg.Caption, msg.Content, msg.Button, msg.Icon);

            confirmWindow.Closed += (sender, e) =>
            {
                if (confirmWindow.DialogResult == true)
                {
                    msg.ProcessCallback(MessageBoxResult.Yes);
                }
                else
                {
                    msg.ProcessCallback(MessageBoxResult.No);
                }
            };

            confirmWindow.Show();
        }

        #endregion Methods

        /// <summary>
        /// Handles the RowLoaded event of the WorkflowDefinitionsGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Windows.Controls.GridView.RowLoadedEventArgs" /> instance containing the event data.</param>
        private void WorkflowDefinitionsGridView_RowLoaded(object sender, RowLoadedEventArgs e)
        {
            WorkflowDefinition definition = e.Row.DataContext as WorkflowDefinition;
            if (definition != null)
            {
                if (!definition.HasChild)
                {
                    ((TreeListViewRow)e.Row).IsExpandable = false;
                }
                else
                {
                    ((TreeListViewRow)e.Row).IsExpandable = true;
                }
            }
        }

        /// <summary>
        /// Handles the RowIsExpandedChanged event of the WorkflowDefinitionsGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Telerik.Windows.Controls.GridView.RowEventArgs" /> instance containing the event data.</param>
        private void WorkflowDefinitionsGridView_RowIsExpandedChanged(object sender, RowEventArgs e)
        {
            WorkflowDefinition definition = e.Row.Item as WorkflowDefinition;
            if (definition != null && (definition.Childs == null || (definition.Childs != null && definition.Childs.Count() == 0)))
            {
                ManageWorkflowTabViewModel context = ((ManageWorkflowTabViewModel)DataContext);
                context.LoadChildren(definition);
            }
        }


    }
}